package com.maimang.platform.controller.system;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.entity.SysDept;
import com.maimang.platform.common.entity.SysDeptTree;
import com.maimang.platform.common.entity.SysRole;
import com.maimang.platform.common.service.SysDeptService;
import com.maimang.platform.common.utils.ShiroUtils;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.lang.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Rocky
 * @version 1.0.0
 * @类名 SysDeptController
 * @描述 <br>部门信息</br>
 * @date 2019-04-23
 * @email jintaozhao@qq.com
 */
@Api(value = "部门信息", description = "部门信息配置操作接口")
@Controller
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController {
	private String prefix = "system/dept";

	@Autowired
	private SysDeptService deptService;

	@RequiresPermissions("system:dept:view")
	@GetMapping()
	public String dept() {
		return prefix + "/dept";
	}

	@RequiresPermissions("system:dept:list")
	@GetMapping("/list")
	@ResponseBody
	public List<SysDept> list(SysDept dept) {
		List<SysDept> deptList = deptService.selectDeptList(dept);
		return deptList;
	}

	/**
	 * 新增部门
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Long parentId, ModelMap modelMap) {
		SysDept dept = deptService.selectDeptById(parentId);
		dept = (dept == null) ? new SysDept() : dept;
		modelMap.put("dept", dept);
		return prefix + "/add";
	}

	/**
	 * 新增保存部门
	 */
	@Log(title = "部门管理", businessType = BusinessType.INSERT)
	@ApiOperation(value = "新增部门", notes = "保存新增部门信息")
	@RequiresPermissions("system:dept:add")
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(SysDept dept) {
		dept.setCreateUser(ShiroUtils.getLoginName());
		return toResult(deptService.insertDept(dept));
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{deptId}")
	public String edit(@PathVariable("deptId") Long deptId, ModelMap modelMap) {
		SysDept dept = deptService.selectDeptById(deptId);
		if (StringUtils.isNotNull(dept) && 1L == deptId) {
			dept.setParentName("无");
		}
		modelMap.put("dept", dept);
		return prefix + "/edit";
	}

	/**
	 * 保存
	 */
	@Log(title = "部门管理", businessType = BusinessType.UPDATE)
	@ApiOperation(value = "")
	@RequiresPermissions("system:dept:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(SysDept dept) {
		dept.setUpdateUser(ShiroUtils.getLoginName());
		return toResult(deptService.updateDept(dept));
	}

	/**
	 * 删除
	 */
	@Log(title = "部门管理", businessType = BusinessType.DELETE)
	@ApiOperation(value = "删除部门", notes = "根据部门id删除部门，若部门存在用户或下级部门则不允许删除")
	@RequiresPermissions("system:dept:remove")
	@PostMapping("/remove/{deptId}")
	@ResponseBody
	public Result remove(@PathVariable("deptId") Long deptId) {
		if (deptService.selectDeptCount(deptId) > 0) {
			return error(1, "存在下级部门,不允许删除");
		}
		if (deptService.checkDeptExistUser(deptId)) {
			return error(1, "部门存在用户,不允许删除");
		}
		return toResult(deptService.deleteDeptById(deptId));
	}

	/**
	 * 校验部门名称
	 */
	@PostMapping("/checkDeptNameUnique")
	@ApiOperation(value = "校验部门", notes = "校验部门名称唯一性")
	@ResponseBody
	public String checkDeptNameUnique(SysDept dept) {
		return deptService.checkDeptNameUnique(dept);
	}

	/**
	 * 校验部门名称
	 */
	@PostMapping("/checkDeptHasChildren/{deptId}")
	@ApiOperation(value = "校验部门", notes = "校验部门是否是一级部门")
	@ResponseBody
	public String checkDeptHasChildren(@PathVariable(name = "deptId") Long deptId) {
		SysDept dept = deptService.selectDeptById(deptId);
		if (StringUtils.isNotNull(dept) && dept.getAncestors().equalsIgnoreCase("0,1")) {
			return "0";
		}
		// return "0";
		return "1";
	}

	/**
	 * 选择部门树
	 */
	@GetMapping("/selectDeptTree/{deptId}")
	public String selectDeptTree(@PathVariable("deptId") Long deptId, ModelMap modelMap) {
		SysDept dept = deptService.selectDeptById(deptId);
		dept = (dept == null) ? new SysDept() : dept;
		modelMap.put("dept", dept);
		return prefix + "/tree";
	}

	/**
	 * 加载部门列表树
	 */
	@GetMapping("/treeData")
	@ApiOperation(value = "部门列表", notes = "加载部门列表树信息")
	@ResponseBody
	public List<Map<String, Object>> treeData() {
		List<Map<String, Object>> tree = deptService.selectDeptTree(new SysDept());
		return tree;
	}

	/**
	 * 加载角色部门（数据权限）列表树
	 */
	@GetMapping("/roleDeptTreeData")
	@ApiOperation(value = "部门角色", notes = "加载角色部门（数据权限）列表树")
	@ResponseBody
	public List<Map<String, Object>> deptTreeData(SysRole role) {
		List<Map<String, Object>> tree = deptService.roleDeptTreeData(role);
		return tree;
	}
}
